<?php

namespace App\Console\Commands;

use App\Http\Controllers\App\PayController;
use App\Model\UserRecharge;
use Illuminate\Console\Command;
use Yansongda\Pay\Pay;

/**
 * 异步判断微信支付成功
 * Class WechatPayStatus
 * @package App\Console\Commands
 */
class WechatPayStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wechatPayStatus';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     * @throws \Exception
     */
    public function handle()
    {
        while (1) {
            // 查询会员status为0 的 和 is_pay为0  的，向微信请求是否是已经支付了
            $user_recharges = UserRecharge::where('status', '0')->where('is_pay', '0')->where(function ($query) {
                $query->orWhere('pay_type', '支付宝支付')->orWhere('pay_type', '微信支付')->orWhere('pay_type', '公众号支付');
            })->orderBy('id', 'DESC')->first();

            if ($user_recharges) {

                if ($user_recharges->pay_type == '支付宝支付') {
                    $alipay = Pay::alipay([
                        'app_id' =>env('ALI_APP_ID'),
                         'ali_public_key' =>env('ALI_PUBLIC_KEY'),
                         'private_key' =>env('ALI_PRIVATE_KEY'),
                    ]);
                    $order = [
                        'out_trade_no' => $user_recharges->out_trade_no
                    ];
                    $result = $alipay->find($order);
                    $callback = $result->all();
                    if ($callback['trade_status'] === 'TRADE_SUCCESS') {
                        // 修改支付状态
                        $pay = new PayController();
                        $pay->vipNotify([
                            'out_trade_no' => $user_recharges->out_trade_no,
                            'total_fee' => $callback['total_amount'] * 100
                        ]);
                    } else {
                        // 修改支付状态
                        $user_recharges->status = 1;
                        $user_recharges->save();
                    }
                } elseif ($user_recharges->pay_type == '微信支付') {
                    $wechat = Pay::wechat([
                        'app_id' => env('WECHAT_APPID'),
                        'mch_id' => env('WECHAT_MCH_ID'),
                        'key' => env('WECHAT_KEY'),
                    ]);
                    $order = [
                        'out_trade_no' => $user_recharges->out_trade_no,
                    ];

                    $result = $wechat->find($order);
                    $callback = $result->all();
                    if ($callback['result_code'] === 'SUCCESS' && $callback['trade_state'] === 'SUCCESS') {
                        // 修改支付状态
                        $pay = new PayController();
                        $pay->vipNotify([
                            'out_trade_no' => $user_recharges->out_trade_no,
                            'total_fee' => $result->total_fee
                        ]);
                    } else {
                        // 修改支付状态
                        $user_recharges->status = 1;
                        $user_recharges->save();
                    }
                } elseif ($user_recharges->pay_type == '公众号支付') {
                    $conf = \Config::get('wechat.payment.default');
                    $wechat = Pay::wechat([
                        'app_id' => $conf['app_id'],
                        'mch_id' => $conf['mch_id'],
                        'key' => $conf['key'],
                    ]);
                    $order = [
                        'out_trade_no' => $user_recharges->out_trade_no,
                    ];
                    $result = $wechat->find($order);
                    $callback = $result->all();
                    if ($callback['result_code'] === 'SUCCESS' && $callback['trade_state'] === 'SUCCESS') {
                        // 修改支付状态
                        $pay = new PayController();
                        $pay->vipNotify([
                            'out_trade_no' => $user_recharges->out_trade_no,
                            'total_fee' => $result->total_fee
                        ]);
                    } else {
                        // 修改支付状态
                        $user_recharges->status = 1;
                        $user_recharges->save();
                    }
                }
            } else {
                echo 'game over';
                sleep(10);
            }
        }
    }
}
